package com.android.hidephotosplus.db;

import java.io.File;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.android.hidephotosplus.model.ModelAlbum;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.model.ModelMediaOfAlbum;
import com.android.hidephotosplus.util.Config;

public class DbHelper {

	private static DbHelper mDbHelper = null;

	private Context context;

	public static SQLiteDatabase mDB;

	public static final String DATABASE_NAME = "hidephotoplus.db";

	public static final int DATABASE_VERSION = 1;

	public static final String TABLE_ALBUM = "tbAlbum";

	public static final String TABLE_MEDIA = "tbMedia";

	public static final String TABLE_PROFILE = "tbProfile";

	public static final String ID = "_id";

	public static final String ID_ALBUM = "_id_album";

	public static final String NAME_ALBUM = "name_album";

	public static final String PATH_ALBUM = "path_album";

	public static final String ID_MEDIA = "_id_media";

	public static final String PATH_MEDIA_HIDDEN = "path_media_hidden";

	public static final String PATH_MEDIA_STOCK = "path_media_stock";

	public static final String NAME_MEDIA = "name_media";

	public static final String TYPE_MEDIA = "type_media";

	public static final String TYPE_STORAGE = "type_storage";

	public static final String KEY = "key";

	public static final String VALUE = "value";

	public static final String DATE = "date";

	public static final String KEY_CLASSIC_PASSWORD = "key_classic_pass";

	public static final String KEY_PATTERN_PASSWORD = "key_pattern_pass";

	public static final String KEY_EMAIL = "key_email";

	public DbHelper(Context context) {
		this.context = context;
		String path = Config.getPathFinal(Config.TYPE_STORAGE_INTERNAL);
		File filePath = new File(path);
		if (!filePath.exists())
			filePath.mkdirs();
		DatabaseHelper helper = new DatabaseHelper(context, path
				+ DATABASE_NAME, null, DATABASE_VERSION);
		mDB = helper.getWritableDatabase();
	}

	public static DbHelper getInstance(Context context) {
		if (mDbHelper == null) {
			synchronized (DbHelper.class) {
				mDbHelper = new DbHelper(context);
			}
		}

		return mDbHelper;
	}

	private class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sql = " CREATE TABLE " + TABLE_ALBUM + " (" + ID_ALBUM
					+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME_ALBUM
					+ " TEXT, " + PATH_ALBUM + " TEXT, " + TYPE_STORAGE
					+ " INTEGER )  ";
			String sql1 = " CREATE TABLE " + TABLE_MEDIA + " (" + ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + ID_MEDIA
					+ " LONG, " + ID_ALBUM + " LONG, " + PATH_MEDIA_STOCK
					+ " TEXT, " + PATH_MEDIA_HIDDEN + " TEXT, " + NAME_MEDIA
					+ " TEXT, " + TYPE_MEDIA + " INTEGER, " + DATE + " TEXT, "
					+ TYPE_STORAGE + " INTEGER  )  ";
			String sql2 = " CREATE TABLE " + TABLE_PROFILE + " (" + ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY + " TEXT, "
					+ VALUE + " TEXT )  ";
			db.execSQL(sql);
			db.execSQL(sql1);
			db.execSQL(sql2);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

		}

	}

	// --------------------------------------------------------

	public boolean checkExistsAlbum(String nameAlbum) {
		boolean result = false;
		String sql = " select count(*) from " + TABLE_ALBUM + " where "
				+ NAME_ALBUM + " = '" + nameAlbum + "'  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				if (cursor.getInt(0) == 0)
					return false;
				else
					return true;
			}
		}

		return result;
	}

	public ArrayList<String> getAllPathAlbum() {
		ArrayList<String> arrStrings = new ArrayList<String>();
		String sql = " select " + PATH_ALBUM + " from " + TABLE_ALBUM + "  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				arrStrings.add(cursor.getString(0));
			}
		}
		tryClose(cursor);
		return arrStrings;
	}

	public void setValueMedia(ModelMedia media, ContentValues values) {
		values.put(ID_MEDIA, media.getIdMedia());
		values.put(ID_ALBUM, media.getIdAlbum());
		values.put(PATH_MEDIA_STOCK, media.getPathMediaStock());
		values.put(PATH_MEDIA_HIDDEN, media.getPathMediaHidden());
		values.put(NAME_MEDIA, media.getNameMedia());
		values.put(TYPE_MEDIA, media.getTypeMedia());
		values.put(DATE, media.getDate());

	}

	public long insertAlbum(String nameAlbum, String pathAlbum, int typeStorage) {
		long id = 0;
		ContentValues values = new ContentValues();
		values.put(NAME_ALBUM, nameAlbum);
		values.put(PATH_ALBUM, pathAlbum);
		values.put(TYPE_STORAGE, typeStorage);
		id = mDB.insert(TABLE_ALBUM, null, values);
		return id;
	}

	public void updateAlbum(long idAlbum, String pathAlbum, int typeStorage) {
		ContentValues values = new ContentValues();
		values.put(PATH_ALBUM, pathAlbum);
		values.put(TYPE_STORAGE, typeStorage);
		mDB.update(TABLE_ALBUM, values, "  " + ID_ALBUM + "='" + idAlbum
				+ "'  ", null);
	}

	public long insertMedia(ModelMedia media) {
		long id = 0;
		ContentValues values = new ContentValues();
		setValueMedia(media, values);
		id = mDB.insert(TABLE_MEDIA, null, values);
		return id;
	}

	public void updateMedia(ModelMedia media) {
		ContentValues values = new ContentValues();
		setValueMedia(media, values);
		mDB.update(TABLE_MEDIA, values,
				" " + ID_MEDIA + " = '" + media.getIdMedia() + "'  ", null);
	}

	public ArrayList<ModelAlbum> getAllAlbum() {
		ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();
		String sql = " select " + ID_ALBUM + "," + NAME_ALBUM + ","
				+ PATH_ALBUM + " from " + TABLE_ALBUM + " ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				item.setIdAlbum(cursor.getLong(0));
				item.setNameAlbum(cursor.getString(1));
				item.setPathAlbum(cursor.getString(2));
				item.setMediaOfAlbum(getMediaOfAlbum(item.getIdAlbum()));
				item.setNumberMedia(getNumberPhoto(item.getIdAlbum())
						+ getNumberVideo(item.getIdAlbum()));
				arrAlbums.add(item);
			}
		}
		tryClose(cursor);
		return arrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbum(int typeStorage) {
		ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();
		String sql = " select " + ID_ALBUM + "," + NAME_ALBUM + ","
				+ PATH_ALBUM + " from " + TABLE_ALBUM + " where "
				+ TYPE_STORAGE + " = '" + typeStorage + "' ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				item.setIdAlbum(cursor.getLong(0));
				item.setNameAlbum(cursor.getString(1));
				item.setPathAlbum(cursor.getString(2));
				item.setMediaOfAlbum(getMediaOfAlbum(item.getIdAlbum()));
				item.setNumberMedia(getNumberPhoto(item.getIdAlbum())
						+ getNumberVideo(item.getIdAlbum()));
				arrAlbums.add(item);
			}
		}
		tryClose(cursor);
		return arrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumPhotos() {
		ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();
		String sql = " select " + ID_ALBUM + "," + NAME_ALBUM + ","
				+ PATH_ALBUM + " from " + TABLE_ALBUM + " ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				ArrayList<ModelMedia> arrMedias = getAllMedia(cursor.getLong(0));
				boolean isExists = false;
				for (ModelMedia media : arrMedias) {
					if (media.getTypeMedia() == Config.TYPE_PHOTO) {
						isExists = true;
						break;
					}
				}
				if (isExists) {
					item.setIdAlbum(cursor.getLong(0));
					item.setNameAlbum(cursor.getString(1));
					item.setPathAlbum(cursor.getString(2));
					item.setMediaOfAlbum(getMediaOfAlbumPhoto(item.getIdAlbum()));
					item.setNumberMedia(getNumberPhoto(item.getIdAlbum()));
					arrAlbums.add(item);
				}
			}
		}
		tryClose(cursor);
		return arrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumVideos(
			ArrayList<ModelAlbum> arrAlbumsPhoto) {
		ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();
		String sql = " select " + ID_ALBUM + "," + NAME_ALBUM + ","
				+ PATH_ALBUM + " from " + TABLE_ALBUM + " ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				// TODO
				boolean isExistsAlbum = false;
				if (arrAlbumsPhoto != null)
					for (ModelAlbum mItem : arrAlbumsPhoto) {
						if (mItem.getNameAlbum().equals(cursor.getString(1))) {
							isExistsAlbum = true;
						}
					}
				if (!isExistsAlbum) {
					ArrayList<ModelMedia> arrMedias = getAllMedia(cursor
							.getLong(0));
					boolean isExists = false;
					for (ModelMedia media : arrMedias) {
						if (media.getTypeMedia() == Config.TYPE_VIDEO) {
							isExists = true;
							break;
						}
					}
					if (isExists) {
						item.setIdAlbum(cursor.getLong(0));
						item.setNameAlbum(cursor.getString(1));
						item.setPathAlbum(cursor.getString(2));
						item.setMediaOfAlbum(getMediaOfAlbumVideo(item
								.getIdAlbum()));
						item.setNumberMedia(getNumberVideo(item.getIdAlbum()));
						arrAlbums.add(item);
					}
				}
			}

		}
		tryClose(cursor);
		return arrAlbums;
	}

	public ArrayList<ModelMedia> getAllMedia(long idAlbum) {
		ArrayList<ModelMedia> arrMedias = new ArrayList<ModelMedia>();
		arrMedias.addAll(getAllPhotos(idAlbum));
		arrMedias.addAll(getAllVideos(idAlbum));
		return arrMedias;
	}

	public ArrayList<ModelMedia> getAllPhotos(long idAlbum) {
		ArrayList<ModelMedia> arrMedias = new ArrayList<ModelMedia>();
		Cursor cursor = mDB.query(TABLE_MEDIA, new String[] { PATH_MEDIA_STOCK,
				PATH_MEDIA_HIDDEN, NAME_MEDIA, TYPE_MEDIA, ID_MEDIA, DATE },
				" " + ID_ALBUM + " ='" + idAlbum + "' AND " + TYPE_MEDIA
						+ " ='" + Config.TYPE_PHOTO + "' ", null, null, null,
				" " + DATE + " DESC ");
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelMedia item = new ModelMedia();
				item.setIdMedia(cursor.getLong(4));
				item.setPathMediaStock(cursor.getString(0));
				item.setPathMediaHidden(cursor.getString(1));
				item.setNameMedia(cursor.getString(2));
				item.setTypeMedia(cursor.getInt(3));
				item.setIdAlbum(idAlbum);
				item.setDate(cursor.getString(5));
				if (item.getTypeMedia() == Config.TYPE_PHOTO)
					item.setVideo(false);
				else
					item.setVideo(true);
				arrMedias.add(item);
			}
		}
		tryClose(cursor);
		return arrMedias;
	}

	public ArrayList<ModelMedia> getAllVideos(long idAlbum) {
		ArrayList<ModelMedia> arrMedias = new ArrayList<ModelMedia>();
		Cursor cursor = mDB.query(TABLE_MEDIA, new String[] { PATH_MEDIA_STOCK,
				PATH_MEDIA_HIDDEN, NAME_MEDIA, TYPE_MEDIA, ID_MEDIA, DATE },
				" " + ID_ALBUM + " ='" + idAlbum + "' AND " + TYPE_MEDIA
						+ " ='" + Config.TYPE_VIDEO + "' ", null, null, null,
				" " + DATE + " DESC ");
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelMedia item = new ModelMedia();
				item.setIdMedia(cursor.getLong(4));
				item.setPathMediaStock(cursor.getString(0));
				item.setPathMediaHidden(cursor.getString(1));
				item.setNameMedia(cursor.getString(2));
				item.setTypeMedia(cursor.getInt(3));
				item.setDate(cursor.getString(5));
				if (item.getTypeMedia() == Config.TYPE_PHOTO)
					item.setVideo(false);
				else
					item.setVideo(true);
				arrMedias.add(item);
			}
		}
		tryClose(cursor);
		return arrMedias;
	}

	public int getNumberPhoto(long idAlbum) {
		int result = 0;
		String sql = " select count(*) from " + TABLE_MEDIA + " where "
				+ ID_ALBUM + " = '" + idAlbum + "' AND  " + TYPE_MEDIA + " = '"
				+ Config.TYPE_PHOTO + "'  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				result = cursor.getInt(0);
			}
		}
		tryClose(cursor);
		return result;
	}

	public int getNumberVideo(long idAlbum) {
		int result = 0;
		String sql = " select count(*) from " + TABLE_MEDIA + " where "
				+ ID_ALBUM + " = '" + idAlbum + "' AND  " + TYPE_MEDIA + " = '"
				+ Config.TYPE_VIDEO + "'  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				result = cursor.getInt(0);
			}
		}
		tryClose(cursor);
		return result;
	}

	private void tryClose(Cursor cursor) {
		try {
			cursor.close();
		} catch (Exception e) {
			Log.e("fail close cursor", "");
		}
	}

	public long getIdAlbumByNameAlbum(String nameAlbum) {
		long idAlbum = 0;
		String sql = " select " + ID_ALBUM + " from " + TABLE_ALBUM + " where "
				+ NAME_ALBUM + " = '" + nameAlbum + "'  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				idAlbum = cursor.getLong(0);
			}
		}
		tryClose(cursor);
		return idAlbum;
	}

	public void deleteMediaById(long idMedia) {
		mDB.delete(TABLE_MEDIA, " " + ID_MEDIA + " = '" + idMedia + "' ", null);
	}

	public void deleteAlbumById(long idAlbum) {
		mDB.delete(TABLE_ALBUM, " " + ID_ALBUM + " = '" + idAlbum + "' ", null);
	}

	public ArrayList<ModelAlbum> getAllAlbumByNameAlbum(String nameAlbum) {
		ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();
		arrAlbums.addAll(getAllAlbumPhotosByNameAlbum(true, nameAlbum));
		arrAlbums
				.addAll(getAllAlbumVideosNameAlbum(true, nameAlbum, arrAlbums));
		return arrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumPhotosByNameAlbum(
			boolean isAllAlbum, String nameAlbum) {
		ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();
		String sql = " select " + ID_ALBUM + "," + NAME_ALBUM + ","
				+ PATH_ALBUM + " from " + TABLE_ALBUM + " where " + NAME_ALBUM
				+ " ='" + nameAlbum + "'  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				ArrayList<ModelMedia> arrMedias = getAllMedia(cursor.getLong(0));
				boolean isExists = false;
				for (ModelMedia media : arrMedias) {
					if (media.getTypeMedia() == Config.TYPE_PHOTO) {
						isExists = true;
						break;
					}
				}
				if (isExists) {
					item.setIdAlbum(cursor.getLong(0));
					item.setNameAlbum(cursor.getString(1));
					item.setPathAlbum(cursor.getString(2));
					item.setMediaOfAlbum(getMediaOfAlbumPhoto(item.getIdAlbum()));
					if (isAllAlbum)
						item.setNumberMedia(getNumberPhoto(item.getIdAlbum())
								+ getNumberVideo(item.getIdAlbum()));
					else
						item.setNumberMedia(getNumberPhoto(item.getIdAlbum()));
					arrAlbums.add(item);
				}
			}
		}
		tryClose(cursor);
		return arrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumVideosNameAlbum(boolean isAllAlbum,
			String nameAlbum, ArrayList<ModelAlbum> arrAlbumsPhoto) {
		ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();
		String sql = " select " + ID_ALBUM + "," + NAME_ALBUM + ","
				+ PATH_ALBUM + " from " + TABLE_ALBUM + " where " + NAME_ALBUM
				+ " ='" + nameAlbum + "'  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				boolean isExistsAlbum = false;
				if (arrAlbumsPhoto != null)
					for (ModelAlbum mItem : arrAlbumsPhoto) {
						if (mItem.getNameAlbum().equals(cursor.getString(1))) {
							isExistsAlbum = true;
						}
					}
				if (!isExistsAlbum) {
					ArrayList<ModelMedia> arrMedias = getAllMedia(cursor
							.getLong(0));
					boolean isExists = false;
					for (ModelMedia media : arrMedias) {
						if (media.getTypeMedia() == Config.TYPE_VIDEO) {
							isExists = true;
							break;
						}
					}
					if (isExists) {
						item.setIdAlbum(cursor.getLong(0));
						item.setNameAlbum(cursor.getString(1));
						item.setPathAlbum(cursor.getString(2));
						item.setMediaOfAlbum(getMediaOfAlbumVideo(item
								.getIdAlbum()));
						if (isAllAlbum)
							item.setNumberMedia(getNumberPhoto(item
									.getIdAlbum())
									+ getNumberVideo(item.getIdAlbum()));
						else
							item.setNumberMedia(getNumberVideo(item
									.getIdAlbum()));
						arrAlbums.add(item);
					}
				}

			}
		}
		tryClose(cursor);
		return arrAlbums;
	}

	public ModelMediaOfAlbum getMediaOfAlbum(long idAlbum) {
		ModelMediaOfAlbum mediaOfAlbum = getMediaOfAlbumPhoto(idAlbum);
		if (mediaOfAlbum.getPathMediaHidden4() == null
				|| Config.NONE.equals(mediaOfAlbum.getPathMediaHidden4())) {
			mediaOfAlbum = getMediaOfAlbumVideo(idAlbum);
		} else {
			Log.e("111", "2222");
			if (mediaOfAlbum.getPathMediaHidden3() == null
					|| Config.NONE.equals(mediaOfAlbum.getPathMediaHidden3())
					|| mediaOfAlbum.getPathMediaHidden2() == null
					|| Config.NONE.equals(mediaOfAlbum.getPathMediaHidden2())
					|| mediaOfAlbum.getPathMediaHidden() == null
					|| Config.NONE.equals(mediaOfAlbum.getPathMediaHidden())) {
				Cursor cursorVideo = mDB.query(TABLE_MEDIA, new String[] {
						PATH_MEDIA_HIDDEN, ID_MEDIA, }, " " + ID_ALBUM + " = '"
						+ idAlbum + "' AND " + TYPE_MEDIA + "='"
						+ Config.TYPE_VIDEO + "' ", null, null, null, " "
						+ DATE + " DESC limit 3");
				if (cursorVideo != null && cursorVideo.getCount() > 0) {
					Log.e("cursorVideo", cursorVideo.getCount() + "");
					while (cursorVideo.moveToNext()) {
						if (mediaOfAlbum.getPathMediaHidden3() == null
								|| Config.NONE.equals(mediaOfAlbum
										.getPathMediaHidden3())) {
							mediaOfAlbum.setIdMedia3(cursorVideo.getLong(1));
							mediaOfAlbum.setPathMediaHidden3(cursorVideo
									.getString(0));
							mediaOfAlbum.setVideo3(true);
							mediaOfAlbum.setIdMedia2(-1);
							mediaOfAlbum.setPathMediaHidden2(Config.NONE);
							mediaOfAlbum.setVideo2(false);
							mediaOfAlbum.setIdMedia(-1);
							mediaOfAlbum.setPathMediaHidden(Config.NONE);
							mediaOfAlbum.setVideo(false);
						} else if (mediaOfAlbum.getPathMediaHidden2() == null
								|| Config.NONE.equals(mediaOfAlbum
										.getPathMediaHidden2())) {
							mediaOfAlbum.setIdMedia2(cursorVideo.getLong(1));
							mediaOfAlbum.setPathMediaHidden2(cursorVideo
									.getString(0));
							mediaOfAlbum.setVideo2(true);
							mediaOfAlbum.setIdMedia(-1);
							mediaOfAlbum.setPathMediaHidden(Config.NONE);
							mediaOfAlbum.setVideo(false);
						} else if (mediaOfAlbum.getPathMediaHidden() == null
								|| Config.NONE.equals(mediaOfAlbum
										.getPathMediaHidden())) {
							mediaOfAlbum.setIdMedia(cursorVideo.getLong(1));
							mediaOfAlbum.setPathMediaHidden(cursorVideo
									.getString(0));
							mediaOfAlbum.setVideo(true);
						}
					}
				}
				tryClose(cursorVideo);
			}

		}
		return mediaOfAlbum;
	}

	public ModelMediaOfAlbum getMediaOfAlbumPhoto(long idAlbum) {
		ModelMediaOfAlbum mediaOfAlbum = new ModelMediaOfAlbum();
		Cursor cursor = mDB.query(TABLE_MEDIA, new String[] {
				PATH_MEDIA_HIDDEN, ID_MEDIA }, " " + ID_ALBUM + " = '"
				+ idAlbum + "' AND " + TYPE_MEDIA + "='" + Config.TYPE_PHOTO
				+ "' ", null, null, null, " " + DATE + " DESC limit 4");

		if (cursor != null && cursor.getCount() == 0) {
			mediaOfAlbum.setPathMediaHidden4(Config.NONE);
			mediaOfAlbum.setIdMedia4(-1);
			mediaOfAlbum.setVideo4(false);
			mediaOfAlbum.setPathMediaHidden3(Config.NONE);
			mediaOfAlbum.setIdMedia3(-1);
			mediaOfAlbum.setVideo3(false);
			mediaOfAlbum.setPathMediaHidden2(Config.NONE);
			mediaOfAlbum.setIdMedia2(-1);
			mediaOfAlbum.setVideo2(false);
			mediaOfAlbum.setPathMediaHidden(Config.NONE);
			mediaOfAlbum.setIdMedia(-1);
			mediaOfAlbum.setVideo(false);
		} else {
			if (cursor != null && cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					if (mediaOfAlbum.getPathMediaHidden4() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden4())) {
						mediaOfAlbum.setPathMediaHidden4(cursor.getString(0));
						mediaOfAlbum.setIdMedia4(cursor.getLong(1));
						mediaOfAlbum.setVideo4(false);

						mediaOfAlbum.setPathMediaHidden3(Config.NONE);
						mediaOfAlbum.setIdMedia3(-1);
						mediaOfAlbum.setVideo3(false);

						mediaOfAlbum.setPathMediaHidden2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);

						mediaOfAlbum.setPathMediaHidden(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaHidden3() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden3())) {
						mediaOfAlbum.setPathMediaHidden3(cursor.getString(0));
						mediaOfAlbum.setIdMedia3(cursor.getLong(1));
						mediaOfAlbum.setVideo3(false);
						mediaOfAlbum.setPathMediaHidden2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaHidden(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaHidden2() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden2())) {
						mediaOfAlbum.setPathMediaHidden2(cursor.getString(0));
						mediaOfAlbum.setIdMedia2(cursor.getLong(1));
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaHidden(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaHidden() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden())) {
						mediaOfAlbum.setPathMediaHidden(cursor.getString(0));
						mediaOfAlbum.setIdMedia(cursor.getLong(1));
						mediaOfAlbum.setVideo(false);
					}
				}
			}
			tryClose(cursor);
			// if (mediaOfAlbum.getPathMediaHidden3() == null
			// || Config.NONE.equals(mediaOfAlbum.getPathMediaHidden3())) {
			// Cursor cursorVideo = mDB.query(TABLE_MEDIA, new String[] {
			// PATH_MEDIA_HIDDEN, ID_MEDIA, }, " " + ID_ALBUM + " = '"
			// + idAlbum + "' AND " + TYPE_MEDIA + "='"
			// + Config.TYPE_VIDEO + "' ", null, null, null, " "
			// + DATE + " DESC limit 3");
			// if (cursorVideo != null && cursorVideo.getCount() > 0) {
			// if (cursorVideo != null && cursorVideo.getCount() > 0) {
			// while (cursorVideo.moveToNext()) {
			// if (mediaOfAlbum.getPathMediaHidden3() == null
			// || Config.NONE.equals(mediaOfAlbum
			// .getPathMediaHidden3())) {
			// mediaOfAlbum
			// .setIdMedia3(cursorVideo.getLong(1));
			// mediaOfAlbum.setPathMediaHidden3(cursorVideo
			// .getString(0));
			// mediaOfAlbum.setVideo3(true);
			// mediaOfAlbum.setIdMedia2(-1);
			// mediaOfAlbum.setPathMediaHidden2(Config.NONE);
			// mediaOfAlbum.setVideo2(true);
			// mediaOfAlbum.setIdMedia(-1);
			// mediaOfAlbum.setPathMediaHidden(Config.NONE);
			// mediaOfAlbum.setVideo(true);
			// } else if (mediaOfAlbum.getPathMediaHidden2() == null
			// || Config.NONE.equals(mediaOfAlbum
			// .getPathMediaHidden2())) {
			// mediaOfAlbum
			// .setIdMedia2(cursorVideo.getLong(1));
			// mediaOfAlbum.setPathMediaHidden2(cursorVideo
			// .getString(0));
			// mediaOfAlbum.setVideo2(true);
			// mediaOfAlbum.setIdMedia(-1);
			// mediaOfAlbum.setPathMediaHidden(Config.NONE);
			// mediaOfAlbum.setVideo(true);
			// } else if (mediaOfAlbum.getPathMediaHidden() == null
			// || Config.NONE.equals(mediaOfAlbum
			// .getPathMediaHidden())) {
			// mediaOfAlbum.setIdMedia(cursorVideo.getLong(1));
			// mediaOfAlbum.setPathMediaHidden(cursorVideo
			// .getString(0));
			// mediaOfAlbum.setVideo(true);
			// }
			// }
			// }
			// }
			// tryClose(cursorVideo);
			// }
		}

		return mediaOfAlbum;
	}

	public ModelMediaOfAlbum getMediaOfAlbumVideo(long idAlbum) {
		ModelMediaOfAlbum mediaOfAlbum = new ModelMediaOfAlbum();
		Cursor cursor = mDB.query(TABLE_MEDIA, new String[] {
				PATH_MEDIA_HIDDEN, ID_MEDIA }, " " + ID_ALBUM + " = '"
				+ idAlbum + "' AND " + TYPE_MEDIA + "='" + Config.TYPE_VIDEO
				+ "' ", null, null, null, " " + DATE + " DESC limit 4");

		if (cursor != null && cursor.getCount() == 0) {
			mediaOfAlbum.setPathMediaHidden4(Config.NONE);
			mediaOfAlbum.setIdMedia4(-1);
			mediaOfAlbum.setVideo4(false);
			mediaOfAlbum.setPathMediaHidden3(Config.NONE);
			mediaOfAlbum.setIdMedia3(-1);
			mediaOfAlbum.setVideo3(false);
			mediaOfAlbum.setPathMediaHidden2(Config.NONE);
			mediaOfAlbum.setIdMedia2(-1);
			mediaOfAlbum.setVideo2(false);
			mediaOfAlbum.setPathMediaHidden(Config.NONE);
			mediaOfAlbum.setIdMedia(-1);
			mediaOfAlbum.setVideo(false);
		} else {
			if (cursor != null && cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					if (mediaOfAlbum.getPathMediaHidden4() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden4())) {
						mediaOfAlbum.setPathMediaHidden4(cursor.getString(0));
						mediaOfAlbum.setIdMedia4(cursor.getLong(1));
						mediaOfAlbum.setVideo4(true);

						mediaOfAlbum.setPathMediaHidden3(Config.NONE);
						mediaOfAlbum.setIdMedia3(-1);
						mediaOfAlbum.setVideo3(false);
						mediaOfAlbum.setPathMediaHidden2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaHidden(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaHidden3() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden3())) {
						mediaOfAlbum.setPathMediaHidden3(cursor.getString(0));
						mediaOfAlbum.setIdMedia3(cursor.getLong(1));
						mediaOfAlbum.setVideo3(true);
						mediaOfAlbum.setPathMediaHidden2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaHidden(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaHidden2() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden2())) {
						mediaOfAlbum.setPathMediaHidden2(cursor.getString(0));
						mediaOfAlbum.setIdMedia2(cursor.getLong(1));
						mediaOfAlbum.setVideo2(true);
						mediaOfAlbum.setPathMediaHidden(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);

					} else if (mediaOfAlbum.getPathMediaHidden() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaHidden())) {
						mediaOfAlbum.setPathMediaHidden(cursor.getString(0));
						mediaOfAlbum.setIdMedia(cursor.getLong(1));
						mediaOfAlbum.setVideo(true);
					}
				}
			}
		}
		return mediaOfAlbum;
	}

	public ModelMedia getMediaById(long idMedia) {
		ModelMedia media = new ModelMedia();
		String sql = " select " + ID_ALBUM + "," + PATH_MEDIA_STOCK + ","
				+ PATH_MEDIA_HIDDEN + "," + NAME_MEDIA + "," + TYPE_MEDIA
				+ " from " + TABLE_MEDIA + " where " + ID_MEDIA + " = '"
				+ idMedia + "' ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			media.setIdMedia(idMedia);
			media.setIdAlbum(cursor.getLong(0));
			media.setPathMediaStock(cursor.getString(1));
			media.setPathMediaHidden(cursor.getString(2));
			media.setNameMedia(cursor.getString(3));
			media.setTypeMedia(cursor.getInt(4));
		}
		tryClose(cursor);
		return media;
	}

	public String getValue(String key) {
		String result = null;
		String sql = " select " + VALUE + " from " + TABLE_PROFILE + " where "
				+ KEY + "='" + key + "'  ";
		Cursor cursor = mDB.rawQuery(sql, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			result = cursor.getString(0);
		}
		tryClose(cursor);
		return result;
	}

	public void insertValue(String key, String value) {
		ContentValues values = new ContentValues();
		values.put(KEY, key);
		values.put(VALUE, value);
		mDB.insert(TABLE_PROFILE, null, values);
	}

	public void updateValue(String key, String newValue) {
		ContentValues values = new ContentValues();
		values.put(VALUE, newValue);
		mDB.update(TABLE_PROFILE, values, " " + KEY + "='" + key + "'  ", null);
	}
}
